package com.tudouji.framework.task;


import com.sun.javafx.binding.StringConstant;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.mt.domain.MtShop;
import com.tudouji.project.mt.mapper.MtShopMapper;
import com.tudouji.project.mt.service.*;
import com.tudouji.project.mt.service.impl.MtFoodconsumServiceImpl;
import com.tudouji.project.mt.service.impl.MtPullService;
import com.tudouji.project.mt.service.impl.MtShopServiceImpl;
import com.tudouji.project.mt.service.impl.MtWhinServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.tudouji.project.mt.domain.MtShop.sell_state;

/**
 * 美团定时任务
 */
@Slf4j
@Component("mtTask")
public class MtTask {

    @Autowired
    private MtShopMapper mtShopMapper;
    @Autowired
    private IMtWhinService mtWhinService;
    @Autowired
    private IMtWhoutService mtWhoutService;
    @Autowired
    private IMtSalesService mtSalesService;
    @Autowired
    private MtFoodconsumServiceImpl mtFoodconsumService;
    @Autowired
    private IMtFoodcostcardService mtFoodcostcardService;

    @Autowired
    private MeituanWhinDataProcessing meituanWhinDataProcessing;

    @Autowired
    private MeituanWhoutDataProcessing meituanWhoutDataProcessing;

    @Autowired
    private MeituanSalesDataProcessing meituanSalesDataProcessing;
    @Autowired
    private MeituanCostcardProcessing meituanCostcardProcessing;
    @Autowired
    protected MeituanDataProcessUtil meituanDataProcessUtil;
    @Autowired
    protected MtPullService mtPullService;

    //美团token刷新
    public void authRefresh() {
        mtPullService.authRefresh();
    }

    //拉取
    public void pull(String tasks) {
        String[] taskList = StringUtils.split(tasks, "-");
        for (String task: taskList){
            if (StringUtils.equalsIgnoreCase(task, "WI")){
                this.whinPull();
            }
            if (StringUtils.equalsIgnoreCase(task, "WO")){
                this.whoutPull();
            }
            if (StringUtils.equalsIgnoreCase(task, "SF")){
                this.salesPull();
            }
            if (StringUtils.equalsIgnoreCase(task, "CT")){
                this.costPull();
            }
//            if (StringUtils.equalsIgnoreCase(task, "MT_PROCESS_CARD")){
//                this.meituanCostcardProcessing.costCardProcess();
//            }
        }
        //清理缓存数据
        meituanDataProcessUtil.clearup();
    }

    //加工
    public void process(String tasks) {
        String[] taskList = StringUtils.split(tasks, "-");
        for (String task: taskList){
            if (StringUtils.equalsIgnoreCase(task, "WI")){
                this.meituanWhinDataProcessing.whinProcess();
            }
            if (StringUtils.equalsIgnoreCase(task, "WO")){
                this.meituanWhoutDataProcessing.whoutProcess();
            }
            if (StringUtils.equalsIgnoreCase(task, "SF")){
                this.meituanCostcardProcessing.costCardProcess();
                this.meituanSalesDataProcessing.salesProcess();
            }
        }
        //清理缓存数据
        meituanDataProcessUtil.clearup();
    }

    //推送
    public void push(String tasks) {
        String[] taskList = StringUtils.split(tasks, "-");
        for (String task: taskList){
            if (StringUtils.equalsIgnoreCase(task, "WI")){
                this.whinDataPush();
            }
            if (StringUtils.equalsIgnoreCase(task, "WO")){
                this.whoutDataPush();
            }
            if (StringUtils.equalsIgnoreCase(task, "SF")){
                this.salesDataPush();
            }
        }
    }

    //消耗单加工
    public void costCardProcess() {
        this.meituanCostcardProcessing.costCardProcess();
    }

    public void baseGYS() throws Exception {
        log.debug("===============基础资料同步：供应商信息START====================");

        log.debug("===============基础资料同步：供应商信息END====================");
    }

    public void baseWL() throws Exception {
        log.debug("===============基础资料同步：物料信息START====================");

        log.debug("===============基础资料同步：物料信息END====================");
    }

    public void baseCP() throws Exception {
        log.debug("===============基础资料同步：菜品信息START====================");

        log.debug("===============基础资料同步：菜品信息END====================");
    }

    public void baseMD() throws Exception {
        log.debug("===============基础资料同步：门店信息START====================");

        log.debug("===============基础资料同步：门店信息END====================");
    }

    public void baseKF() throws Exception {
        log.debug("===============基础资料同步：库房信息START====================");

        log.debug("===============基础资料同步：库房信息END====================");
    }

    public void whinDataPush() {
        log.debug("===============出入库信息：数据推送START====================");
        MtShop mtShop = new MtShop();
//        mtShop.setStatus(100);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtWhinService.whinDataPush(mtShopList);
        log.debug("===============出入库信息：数据推送END====================");
    }

    public void whoutDataPush() {
        log.debug("===============出入库信息：数据推送START====================");
        MtShop mtShop = new MtShop();
//        mtShop.setStatus(100);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtWhoutService.whoutDataPush(mtShopList);
        log.debug("===============出入库信息：数据推送END====================");
    }

    public void xhDataPush() {
        log.debug("===============菜品消耗单：数据推送START====================");

        MtShop mtShop = new MtShop();
        mtShop.setStatus(1);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtFoodconsumService.dataPush(mtShopList);
        log.debug("===============菜品消耗单：数据推送END====================");
    }

    public void salesDataPush() {
        log.debug("===============销售单：数据推送START====================");

        MtShop mtShop = new MtShop();
//        mtShop.setStatus(100);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtSalesService.dataPush(mtShopList);
        log.debug("===============销售单：数据推送END====================");
    }


    //入库单拉取
    private void whinPull() {
        MtShop mtShop = new MtShop();
        mtShop.setStatus(1);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtWhinService.dataPull(mtShopList);
    }
    //出库单拉取
    private void whoutPull() {
        MtShop mtShop = new MtShop();
        mtShop.setStatus(1);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtWhoutService.dataPull(mtShopList);
    }
    //消耗单拉取
    private void costPull() {
        MtShop mtShop = new MtShop();
        mtShop.setStatus(1);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtFoodcostcardService.dataPull(mtShopList);
    }
    //销售单拉取
    private void salesPull() {
        MtShop mtShop = new MtShop();
        mtShop.setStatus(1);
        List<MtShop> mtShopList = mtShopMapper.selectMtShopList(mtShop);
        mtSalesService.dataPull(mtShopList);
    }

}
